home *** CD-ROM | disk | FTP | other *** search
/ CrystalVision Software Se… Wiki Wonder - Wikipedia / CrystalVision Software Services 703: The Wiki Wonder - Wikipedia.iso / 0703 / Educate / Complete Calc / Setup.exe / lib / tile / entry.tcl < prev    next >
Encoding:
Text File  |  2006-10-25  |  9.1 KB  |  405 lines

  1.  
  2. namespace eval tile {
  3. namespace eval entry {
  4. variable State
  5.  
  6. set State(x) 0
  7. set State(selectMode) char
  8. set State(anchor) 0
  9. set State(scanX) 0
  10. set State(scanIndex) 0
  11. set State(scanMoved) 0
  12.  
  13. set State(scanNum) 1
  14. set State(scanDen) 1
  15. set State(deadband) 3    ;# #pixels for mouse-moved deadband.
  16. }
  17. }
  18.  
  19.  
  20. bind TEntry <<Cut>>             { tile::entry::Cut %W }
  21. bind TEntry <<Copy>>             { tile::entry::Copy %W }
  22. bind TEntry <<Paste>>             { tile::entry::Paste %W }
  23. bind TEntry <<Clear>>             { tile::entry::Clear %W }
  24.  
  25. bind TEntry <ButtonPress-1>         { tile::entry::Press %W %x }
  26. bind TEntry <Shift-ButtonPress-1>    { tile::entry::Shift-Press %W %x }
  27. bind TEntry <Double-ButtonPress-1>     { tile::entry::Select %W %x word }
  28. bind TEntry <Triple-ButtonPress-1>     { tile::entry::Select %W %x line }
  29. bind TEntry <B1-Motion>            { tile::entry::Drag %W %x }
  30.  
  31. bind TEntry <B1-Leave>         { tile::Repeatedly tile::entry::AutoScroll %W }
  32. bind TEntry <B1-Enter>        { tile::CancelRepeat }
  33. bind TEntry <ButtonRelease-1>    { tile::CancelRepeat }
  34.  
  35. bind TEntry <Control-ButtonPress-1> {
  36. %W instate {!readonly !disabled} { %W icursor @%x ; focus %W }
  37. }
  38.  
  39. bind TEntry <ButtonPress-2>         { tile::entry::ScanMark %W %x }
  40. bind TEntry <B2-Motion>         { tile::entry::ScanDrag %W %x }
  41. bind TEntry <ButtonRelease-2>        { tile::entry::ScanRelease %W %x }
  42. bind TEntry <<PasteSelection>>        { tile::entry::ScanRelease %W %x }
  43.  
  44. bind TEntry <Key-Left>             { tile::entry::Move %W prevchar }
  45. bind TEntry <Key-Right>         { tile::entry::Move %W nextchar }
  46. bind TEntry <Control-Key-Left>        { tile::entry::Move %W prevword }
  47. bind TEntry <Control-Key-Right>        { tile::entry::Move %W nextword }
  48. bind TEntry <Key-Home>            { tile::entry::Move %W home }
  49. bind TEntry <Key-End>            { tile::entry::Move %W end }
  50.  
  51. bind TEntry <Shift-Key-Left>         { tile::entry::Extend %W prevchar }
  52. bind TEntry <Shift-Key-Right>        { tile::entry::Extend %W nextchar }
  53. bind TEntry <Shift-Control-Key-Left>    { tile::entry::Extend %W prevword }
  54. bind TEntry <Shift-Control-Key-Right>    { tile::entry::Extend %W nextword }
  55. bind TEntry <Shift-Key-Home>        { tile::entry::Extend %W home }
  56. bind TEntry <Shift-Key-End>        { tile::entry::Extend %W end }
  57.  
  58. bind TEntry <Control-Key-slash>     { %W selection range 0 end }
  59. bind TEntry <Control-Key-backslash>     { %W selection clear }
  60.  
  61. bind TEntry <<TraverseIn>>     { %W selection range 0 end; %W icursor end }
  62.  
  63. bind TEntry <KeyPress>             { tile::entry::Insert %W %A }
  64. bind TEntry <Key-Delete>        { tile::entry::Delete %W }
  65. bind TEntry <Key-BackSpace>         { tile::entry::Backspace %W }
  66.  
  67. bind TEntry <Alt-KeyPress>        {# nothing}
  68. bind TEntry <Meta-KeyPress>        {# nothing}
  69. bind TEntry <Control-KeyPress>         {# nothing}
  70. bind TEntry <Key-Escape>         {# nothing}
  71. bind TEntry <Key-Return>         {# nothing}
  72. bind TEntry <Key-KP_Enter>         {# nothing}
  73. bind TEntry <Key-Tab>             {# nothing}
  74.  
  75. if {[tk windowingsystem] eq "aqua"} {
  76. bind TEntry <Command-KeyPress>    {# nothing}
  77. }
  78.  
  79. bind TEntry <Control-Key-a>        { tile::entry::Move %W home }
  80. bind TEntry <Control-Key-b>        { tile::entry::Move %W prevchar }
  81. bind TEntry <Control-Key-d>         { tile::entry::Delete %W }
  82. bind TEntry <Control-Key-e>         { tile::entry::Move %W end }
  83. bind TEntry <Control-Key-f>         { tile::entry::Move %W nextchar }
  84. bind TEntry <Control-Key-h>        { tile::entry::Backspace %W }
  85. bind TEntry <Control-Key-k>        { %W delete insert end }
  86.  
  87.  
  88. proc tile::entry::EntrySelection {w} {
  89. set entryString [string range [$w get] [$w index sel.first]  [expr {[$w index sel.last] - 1}]]
  90. if {[$w cget -show] ne ""} {
  91. return [string repeat [string index [$w cget -show] 0]  [string length $entryString]]
  92. }
  93. return $entryString
  94. }
  95.  
  96. proc tile::entry::Paste {w} {
  97. catch {
  98. set clipboard [::tk::GetSelection $w CLIPBOARD]
  99. PendingDelete $w
  100. $w insert insert $clipboard
  101. See $w insert
  102. }
  103. }
  104.  
  105. proc tile::entry::Copy {w} {
  106. if {![catch {EntrySelection $w} selection]} {
  107. clipboard clear -displayof $w
  108. clipboard append -displayof $w $selection
  109. }
  110. }
  111.  
  112. proc tile::entry::Clear {w} {
  113. catch { $w delete sel.first sel.last }
  114. }
  115.  
  116. proc tile::entry::Cut {w} {
  117. Copy $w; Clear $w
  118. }
  119.  
  120.  
  121. proc tile::entry::ClosestGap {w x} {
  122. set pos [$w index @$x]
  123. set bbox [$w bbox $pos]
  124. if {$x - [lindex $bbox 0] > [lindex $bbox 2]/2} {
  125. incr pos
  126. }
  127. return $pos
  128. }
  129.  
  130. proc tile::entry::See {w {index insert}} {
  131. set c [$w index $index]
  132. if {$c < [$w index @0] || $c >= [$w index @[winfo width $w]]} {
  133. $w xview $c
  134. }
  135. }
  136.  
  137. set ::tile::entry::State(startNext)  [string equal $tcl_platform(platform) "windows"]
  138.  
  139. proc tile::entry::NextWord {w start} {
  140. variable State
  141. set pos [tcl_endOfWord [$w get] [$w index $start]]
  142. if {$pos >= 0 && $State(startNext)} {
  143. set pos [tcl_startOfNextWord [$w get] $pos]
  144. }
  145. if {$pos < 0} {
  146. return end
  147. }
  148. return $pos
  149. }
  150.  
  151. proc tile::entry::PrevWord {w start} {
  152. set pos [tcl_startOfPreviousWord [$w get] [$w index $start]]
  153. if {$pos < 0} {
  154. return 0
  155. }
  156. return $pos
  157. }
  158.  
  159. proc tile::entry::RelIndex {w where {index insert}} {
  160. switch -- $where {
  161. prevchar    { expr {[$w index $index] - 1} }
  162. nextchar    { expr {[$w index $index] + 1} }
  163. prevword    { PrevWord $w $index }
  164. nextword    { NextWord $w $index }
  165. home        { return 0 }
  166. end        { $w index end }
  167. default        { error "Bad relative index $index" }
  168. }
  169. }
  170.  
  171. proc tile::entry::Move {w where} {
  172. $w icursor [RelIndex $w $where]
  173. $w selection clear
  174. See $w insert
  175. }
  176.  
  177.  
  178. proc tile::entry::ExtendTo {w index} {
  179. set index [$w index $index]
  180. set insert [$w index insert]
  181.  
  182. if {![$w selection present]} {
  183. set anchor $insert
  184. } else {
  185. set selfirst [$w index sel.first]
  186. set sellast  [$w index sel.last]
  187.  
  188. if {   ($index < $selfirst)
  189. || ($insert == $selfirst && $index <= $sellast)
  190. } {
  191. set anchor $sellast
  192. } else {
  193. set anchor $selfirst
  194. }
  195. }
  196.  
  197. if {$anchor < $index} {
  198. $w selection range $anchor $index
  199. } else {
  200. $w selection range $index $anchor
  201. }
  202.  
  203. $w icursor $index
  204. return $anchor
  205. }
  206.  
  207. proc tile::entry::Extend {w where} {
  208. ExtendTo $w [RelIndex $w $where]
  209. See $w
  210. }
  211.  
  212.  
  213. proc tile::entry::Press {w x} {
  214. variable State
  215.  
  216. $w icursor [ClosestGap $w $x]
  217. $w selection clear
  218. $w instate {!readonly !disabled} { focus $w }
  219.  
  220. set State(x) $x
  221. set State(selectMode) char
  222. set State(anchor) [$w index insert]
  223. }
  224.  
  225. proc tile::entry::Shift-Press {w x} {
  226. variable State
  227.  
  228. focus $w
  229. set anchor [ExtendTo $w @$x]
  230.  
  231. set State(x) $x
  232. set State(selectMode) char
  233. set State(anchor) $anchor
  234. }
  235.  
  236. proc tile::entry::Select {w x mode} {
  237. variable State
  238. set cur [ClosestGap $w $x]
  239.  
  240. switch -- $mode {
  241. word    { WordSelect $w $cur $cur }
  242. line    { LineSelect $w $cur $cur }
  243. char    { # no-op }
  244. }
  245.  
  246. set State(anchor) $cur
  247. set State(selectMode) $mode
  248. }
  249.  
  250. proc tile::entry::Drag {w x} {
  251. variable State
  252. set State(x) $x
  253. DragTo $w $x
  254. }
  255.  
  256. proc tile::entry::DragTo {w x} {
  257. variable State
  258.  
  259. set cur [ClosestGap $w $x]
  260. switch $State(selectMode) {
  261. char { CharSelect $w $State(anchor) $cur }
  262. word { WordSelect $w $State(anchor) $cur }
  263. line { LineSelect $w $State(anchor) $cur }
  264. }
  265. }
  266.  
  267. proc tile::entry::AutoScroll {w} {
  268. variable State
  269. if {![winfo exists $w]} return
  270. set x $State(x)
  271. if {$x > [winfo width $w]} {
  272. $w xview scroll 2 units
  273. DragTo $w $x
  274. } elseif {$x < 0} {
  275. $w xview scroll -2 units
  276. DragTo $w $x
  277. }
  278. }
  279.  
  280. proc tile::entry::CharSelect {w from to} {
  281. if {$to <= $from} {
  282. $w selection range $to $from
  283. } else {
  284. $w selection range $from $to
  285. }
  286. $w icursor $to
  287. }
  288.  
  289. proc tile::entry::WordSelect {w from to} {
  290. if {$to < $from} {
  291. set first [WordBack [$w get] $to]
  292. set last [WordForward [$w get] $from]
  293. $w icursor $first
  294. } else {
  295. set first [WordBack [$w get] $from]
  296. set last [WordForward [$w get] $to]
  297. $w icursor $last
  298. }
  299. $w selection range $first $last
  300. }
  301.  
  302. proc WordBack {text index} {
  303. if {[set pos [tcl_wordBreakBefore $text $index]] < 0} { return 0 }
  304. return $pos
  305. }
  306. proc WordForward {text index} {
  307. if {[set pos [tcl_wordBreakAfter $text $index]] < 0} { return end }
  308. return $pos
  309. }
  310.  
  311. proc tile::entry::LineSelect {w _ _} {
  312. variable State
  313. $w selection range 0 end
  314. $w icursor end
  315. }
  316.  
  317.  
  318. proc tile::entry::ScanMark {w x} {
  319. variable State
  320. set State(scanX) $x
  321. set State(scanIndex) [$w index @0]
  322. set State(scanMoved) 0
  323. }
  324.  
  325. proc tile::entry::ScanDrag {w x} {
  326. variable State
  327.  
  328. set dx [expr {$State(scanX) - $x}]
  329. if {abs($dx) > $State(deadband)} {
  330. set State(scanMoved) 1
  331. }
  332. set left [expr {$State(scanIndex) + ($dx*$State(scanNum))/$State(scanDen)}]
  333. $w xview $left
  334.  
  335. if {$left != [set newLeft [$w index @0]]} {
  336. set State(scanX) $x
  337. set State(scanIndex) $newLeft
  338. }
  339. }
  340.  
  341. proc tile::entry::ScanRelease {w x} {
  342. variable State
  343. if {!$State(scanMoved)} {
  344. $w instate {!disabled !readonly} {
  345. $w icursor [ClosestGap $w $x]
  346. catch {$w insert insert [::tk::GetSelection $w PRIMARY]}
  347. }
  348. }
  349. }
  350.  
  351.  
  352. proc tile::entry::PendingDelete {w} {
  353. if {[$w selection present]} {
  354. $w icursor sel.first
  355. $w delete sel.first sel.last
  356. return 1
  357. }
  358. return 0
  359. }
  360.  
  361. proc tile::entry::Insert {w s} {
  362. if {$s eq ""} { return }
  363. PendingDelete $w
  364. $w insert insert $s
  365. See $w insert
  366. }
  367.  
  368. proc tile::entry::Backspace {w} {
  369. if {[PendingDelete $w]} {
  370. See $w
  371. return
  372. }
  373. set x [expr {[$w index insert] - 1}]
  374. if {$x < 0} { return }
  375.  
  376. $w delete $x
  377.  
  378. if {[$w index @0] >= [$w index insert]} {
  379. set range [$w xview]
  380. set left [lindex $range 0]
  381. set right [lindex $range 1]
  382. $w xview moveto [expr {$left - ($right - $left)/2.0}]
  383. }
  384. }
  385.  
  386. proc tile::entry::Delete {w} {
  387. if {![PendingDelete $w]} {
  388. $w delete insert
  389. }
  390. }
  391.  
  392. proc tile::entry::compat {w cmd subcmd args} {
  393. switch -glob -- $cmd-$subcmd {
  394. sc*-m* { ScanMark $w [lindex $args 0]    ;# $w scan mark $x }
  395. sc*-d* { ScanDrag $w [lindex $args 0]    ;# $w scan dragto $x }
  396. se*-t* {
  397. CharSelect $w [$w index anchor] [$w index [lindex $args 0]]
  398. }
  399. se*-a* {
  400. ExtendTo $w [lindex $args 0]
  401. }
  402. }
  403. }
  404.  
  405.